package com.tianhui.consignortwofei.pattern.orderOprate.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.fgs.common.CommonModel;
import com.fgs.common.CommonResponse;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.dialog.OperateTipDialog;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.MainActivity;
import com.tianhui.consignortwofei.pattern.orderOprate.IOrderOperate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/30
 * @description
 **/
public class CancelConfirmOperate implements IOrderOperate {
    @Override
    public void orderOperate(Context context, OrderOperateInfo operateInfo) {
        showTerminateDialog(context, operateInfo);
    }

    private void showTerminateDialog(Context context, OrderOperateInfo operateInfo) {
        new OperateTipDialog.Builder(context)
                .title("请输入原因")
                .input("此订单作废", new OperateTipDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull OperateTipDialog dialog, CharSequence input) {
                    }
                })
                .positiveText("确定")
                .negativeText("取消")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        String reason = dialog.getInputContent();
                        if (TextUtils.isEmpty(reason)) {
                            ToastUtil.showShortToast("请输入取消原因");
                            return;
                        }
                        dialog.dismiss();
                        Map<String, String> map = new HashMap<>();
                        map.put("billno", operateInfo.orderInfo.dispatchno);
                        map.put("remark", reason);
                        map.put("isstate", "2");
                        new CommonModel().subscribe(context, CommonConfig.getInstance().create(OrderApiService.class).orderTerminateOperate(map), new ObserverResponseListener<CommonResponse>() {
                            @Override
                            public void onNext(CommonResponse commonResponse) {
                                if (commonResponse.getCode() == 20000) {
                                    showTerminateResultDialog(context, "单据取消成功", operateInfo);
                                    EventBusManage.refreshOrderList();
                                    EventBusManage.activityFinish();
                                } else {
                                    ToastUtil.showShortToast(commonResponse.getMsg());
                                }
                            }

                            @Override
                            public void onError(ExceptionHandle.ResponseThrowable e) {
                                ToastUtil.showShortToast(e.message);
                            }
                        });
                    }
                })
                .show();
    }

    private void showTerminateResultDialog(Context context, String message, OrderOperateInfo operateInfo) {
        new OperateTipDialog.Builder(context)
                .title("提示")
                .content(message)
                .isCancelable(false)
                .positiveText("返回列表")
                .onPositive(new OperateTipDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull OperateTipDialog dialog) {
                        dialog.dismiss();
                        Intent intent = new Intent(context, MainActivity.class);
//                        Intent intent = new Intent(context, OrderListActivity.class);
//                        Bundle bundle = new Bundle();
//                        bundle.putInt(KeyValue.KEY_VALUE, operateInfo.type);
//                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    }
                })
                .show();
    }
}
